package com.leyou.item.controller;

import com.leyou.common.pojo.PageResult;
import com.leyou.item.pojo.Brand;
import com.leyou.item.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("brand")
public class BrandController {
//http://api.leyou.com/api/item/brand/page?key=&page=1&rows=5&sortBy=id&desc=false

    @Autowired
    private BrandService brandService;

    /**
     * 查询品牌页面-返回的是一个已经分页好的数据结果集
     * RP新学习的类--响应实体类org.springframework.http;
     * 分页信息设置默认，可选信息设置required=false,设置为空可能出现错误，比如查询空
     *
     * @param key    查询关键词条可以是name模糊或者是首字母
     * @param page   当前页面
     * @param rows   显示多少条
     * @param sortBy 排序方依据
     * @param desc   desc排序方式 desc--false asc默认是升序true，mysql中1true0false
     * @return {@link ResponseEntity}<{@link PageResult}<{@link Brand}>>
     */
    @GetMapping("page")
    public ResponseEntity<PageResult<Brand>> queryBrandsByPage(
            @RequestParam(value = "key", required = false) String key,
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "rows", defaultValue = "5") Integer rows,
            @RequestParam(value = "sortBy", required = false) String sortBy,
            @RequestParam(value = "desc", required = false) Boolean desc
    ) {
//        参数处理
        //让业务层处理，控制层负责拿数据，虽然参数多但还是这样
        PageResult<Brand> result = this.brandService.queryBrandsByPage(key, page, rows, sortBy, desc);

        if (CollectionUtils.isEmpty(result.getItems())) {//查看返回结果集是否为空
            return ResponseEntity.notFound().build();//没有拿到数据
        }
        return ResponseEntity.ok(result);
    }

    /**
     * 新增品牌-返回值为空，响应状态码就可以post请求方式决定方法映射方式
     *
     * @param brand 接受品牌参数--pojo ?k=v，属性名和实体名一样，可以直接映射
     * @param cids  接受分类id集合,需要手动映射
     */
    @PostMapping
    public ResponseEntity<Void> saveBrand(Brand brand, @RequestParam("cids") List<Long> cids) {
        this.brandService.saveBrand(brand, cids);
        return ResponseEntity.status(HttpStatus.CREATED).build();//body传递参数，build返回结果
    }

    /**
     * 新增商品---查询品牌由cid分类--大量无法直接查询数据，通过分类筛选
     *
     * @param cid cid
     * @return {@link ResponseEntity}<{@link List}<{@link Brand}>>
     */
    @GetMapping("cid/{cid}")
    public ResponseEntity<List<Brand>> queryBrandsByCid(@PathVariable("cid") Long cid) {
        List<Brand> brands = this.brandService.queryBrandsByCid(cid);
        if (CollectionUtils.isEmpty(brands)) {
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(brands);
    }

    /**
     * 查询品牌通过id
     *
     * @param id id
     * @return {@link ResponseEntity}<{@link List}<{@link Brand}>>
     */
    @GetMapping("{id}")
    public ResponseEntity<Brand> queryBrandById(@PathVariable("id") Long id) {
        Brand brand = this.brandService.queryBrandById(id);
        if (brand == null) {
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(brand);
    }
}
